//
//  RTJInvestmentDetailsController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/11.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJInvestmentDetailsController.h"
#import "RTJBidRecordsCell.h"

@interface RTJInvestmentDetailsController ()

@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) NSDictionary *dict;
@property(nonatomic, assign) NSInteger currentPage;

@end

@implementation RTJInvestmentDetailsController

-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
    
}

-(NSMutableArray *)data {
    
    if (!_data) {
        _data = [NSMutableArray array];
    }
    
    return _data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"投资详情";
    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight=300;
    
    self.currentPage = 1;
    self.tableView.mj_header = [CustomRefreshGifHeader headerWithRefreshingBlock:^{
        
        self.currentPage = 1;
        [self loadData];
        
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        self.currentPage ++;
        [self loadData];
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
    
}

-(void)loadData{
    
    [SVProgressHUD  showWithStatus:nil];
    
    if (self.data.count == 0) {
        
        [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
        
    }
    
    
    [RTJNetWork asyncRequestWithURL:@"user/invite_loads" params:@{@"page":@(_currentPage),@"id":_idStr} method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        
        [self.view removeEmptyView];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                NSArray *arr = [NSArray array];
                arr = data[@"data"][@"load_list"];
                
                if (arr.count > 0) {
                    
                    if (self.currentPage == 1) {
                        
                        self.dict = data[@"data"];
                        [self.data removeAllObjects];
                    }
                    
                    [self.data addObjectsFromArray:arr];
                    
                    if (arr.count < ZXPAGE_SIZE) {
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                    
                }else{
                    
                    if (self.currentPage == 1) {
                        
                        
                        [self.data removeAllObjects];
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        
                        [self.view showEmptyViewWithStr:@"暂无相关数据" image:[UIImage imageNamed:@"kbylogo"] callback:^{
                            [self loadData];
                        }];
                        
                    }else{
                        
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                        
                    }
                }
                
                [self.tableView reloadData];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
                [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self loadData];
                }];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self loadData];
            }];

        }
    
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  self.data.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJBidRecordsCell";
    
    RTJBidRecordsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    
        if (!cell) {
            
            cell = [[NSBundle mainBundle] loadNibNamed:@"RTJBidRecordsCell" owner:self options:nil][4];
        }
    
    cell.touziTotal.text = [NSString stringWithFormat:@"%@ 元",_data[indexPath.row][@"money"]];
    cell.touziPeople.text = _data[indexPath.row][@"name"];
    cell.touziZhuce.text = [NSString stringWithFormat:@"出借时间：%@",_data[indexPath.row][@"create_time"]];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return  cell;
    
}



@end
